package com.ce.pms.custservice.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.vo.HmDeptVO;
import com.ce.pms.custservice.service.ICustServChargeService;
import com.ce.pms.custservice.service.IDFormManageService;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.vo.AbstractServiceItemVO;
import com.ce.pms.custservice.vo.BillVO;
import com.ce.pms.custservice.vo.CustServiceEventVO;
import com.ce.pms.custservice.vo.DispatchFormSearchVO;
import com.ce.pms.custservice.vo.DispatchFormVO;
import com.ce.pms.custservice.vo.DispatchingVO;
import com.ce.pms.custservice.vo.LaborItemVO;
import com.ce.pms.custservice.vo.MaterialItemVO;
import com.ce.pms.custservice.vo.ServItemSearchVO;
import com.ce.pms.system.custservice.service.IEmergencyDegreeService;
import com.ce.pms.system.custservice.vo.EmergencyDegreeVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

public class DispatchFormAction extends PmsBaseAction {

	/**
	 * 功能描述:创建派工单
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String addDispatchForm(@Read DispatchFormVO dispatchFormVO,
			@Read(key="customerEventId")Long customerEventId,
			@Read(key="appointment")String appointment){
		dispatchFormVO.setCreator(this.getUserId());
		dispatchFormVO.setDispatchMan(this.getUsername());
		dispatchFormVO.setAppointment(DateUtil.convertStrToDate(appointment,"yyyy-MM-dd HH:mm"));
		dispatchFormManageService.addDispatchForm(customerEventId, dispatchFormVO);
		 this.getRequest().setAttribute("dispatchFormId", dispatchFormVO.getKeyword());
		 this.getRequest().setAttribute("fullAddress", dispatchFormVO.getFullAddress());
		return "loadDispatchFormEdit";
	}


	/**
	 * 功能描述:初始化增加派工单
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String navigateDispatchFormAdd(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId){

		CustServiceEventVO eventVO =  eventManageService.getCustServEventByID(customerEventId);
		DispatchFormVO vo=new DispatchFormVO();
		vo.setHouseNumber(eventVO.getHouseNumber());
		vo.setHouseId(eventVO.getHouseKeyword());
		vo.setContact(eventVO.getContact());
        vo.setLinkPhone(eventVO.getLinkPhone());
        vo.setAppointment(DateUtil.convertStrToDate(eventVO.getAppointment(),"yyyy-MM-dd HH:mm"));
        vo.setEmergencyDegreeId(eventVO.getEmergencyDegreeKeyword());
        vo.setFullAddress(eventVO.getFullAddress());
        vo.setServiceContent(eventVO.getEventContent());
//		vo.setDispatchTime(new Date());
		this.getRequest().setAttribute("dispatchFormVO", vo);
		getEmergencyDegreeList();
		return "dispatchFormEdit";
	}

	/**
	 * 功能描述:初始化修改派工单
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String navigateDispatchFormEdit(){
		Long dispatchFormId=(Long)this.getRequest().getAttribute("dispatchFormId");
		String fullAddress=(String)this.getRequest().getAttribute("fullAddress");

		DispatchFormVO vo=dispatchFormManageService.getDFormByID(dispatchFormId);
		vo.setFullAddress(fullAddress);
		this.getRequest().setAttribute("dispatchFormVO", vo);
		getEmergencyDegreeList();

		return "dispatchFormEdit";
	}

	/**
	 * 功能描述:导航到派工单详细<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateDispatchFormDetail(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){
		DispatchFormVO dispatchFormVO =dispatchFormManageService.getDFormByID(dispatchFormId);
		SysrefcodeDO sys = this.sysrefcodeService.getSysrefCodesByCode(SysrefcodeDO.CE_DISPATCH_FORM_STATUZ_CODE, dispatchFormVO.getStatuz());
		dispatchFormVO.setStatuName(sys.getName());
		this.getRequest().setAttribute("entryVO", dispatchFormVO);

		BillVO billVO = custServChargeService.getBillVOByDispatchFormId(dispatchFormId);
		this.getRequest().setAttribute("billVO", billVO);

		return "dispatchFormDetail";
	}

	/**
	 * 功能描述:初始化派工单作废页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-24<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateDispatchFormDisannul(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){
		return "dispatchFormDisannul";
	}

	/**
	 * 功能描述:保存派工单作废<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-24<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String disannulDispatchForm(@Read(key="dispatchFormId")Long dispatchFormId,
			@Read(key="disannulInfo")String disannulInfo){
		dispatchFormManageService.setAsideDForm(dispatchFormId, disannulInfo);
		this.getRequest().setAttribute("iframeName", "customerDispatchFormList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String dispatchFormCommonMain(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId){
		return "dispatchFormCommonMain";
	}

	/**
	 * 功能描述:
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String dispatchFormMain(){
		HmDeptVO hmDeptVO=organizationalStructureService.queryDeptInfoByUseID(this.getUserId());
		if(!BlankUtil.isBlank(hmDeptVO)){
			this.getRequest().setAttribute("deptId", hmDeptVO.getDeptId());
		}

		return "dispatchFormMain";
	}


	/**
	 * 功能描述:
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String queryDispatchFormByPage(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId,
			@Read(key="deptId")@Out(key="deptId")Long deptId,
			@Read(key="houseNumber")@Out(key="houseNumber")String houseNumber,
			@Read(key="dispatchNo")@Out(key="dispatchNo")String dispatchNo,
			@Read(key="serviceContent")@Out(key="serviceContent")String serviceContent,
			@Read(key="MenuFlat",defaultValue="true")@Out(key="MenuFlat")String MenuFlat,
			@Read(key = "ec_crd",defaultValue="10")int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){
		this.getRequest().setAttribute("pageSize", new Integer(pageSize));
		DispatchFormSearchVO searchVO=new DispatchFormSearchVO();
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);
		searchVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		searchVO.setCustEventId(customerEventId);
		searchVO.setDeptId(deptId);
		searchVO.setHouseNumber(houseNumber);
		searchVO.setDispatchNo(dispatchNo);
		searchVO.setServiceContent(serviceContent);
		Page page=dispatchFormManageService.queryDFromByPage(searchVO);

		List<DispatchFormVO> list = page.getResult();
		Long totalRows = page.getTotalCount();
		this.getRequest().setAttribute("dispatchFormList", list);
		this.getRequest().setAttribute("totalRows", totalRows.intValue());


//		Page page = new Page();
//		List dispathchFormList =initDispathchFormList();
//		page.setResult(dispathchFormList);
//		setAttribute("dispatchFormList", dispathchFormList);
//
//		setAttribute("totalRows", new Integer(dispathchFormList.size()));

		return "dispatchFormList";
	}

	/**
	 * 功能描述:导航到服务费用项目页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateChargeItemManage(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){
		return "chargeItemMain";
	}

	/**
	 * 功能描述:<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String findChargeItemByPage(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId,
			@Read(key="MenuFlat",defaultValue="true")@Out(key="MenuFlat")String MenuFlat,
			@Read(key = "ec_crd",defaultValue="10")int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){
		ServItemSearchVO searchVO= new ServItemSearchVO();
		searchVO.setDispatchFormId(dispatchFormId);
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);
		searchVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		Page page=dispatchFormManageService.queryServItemByPage(searchVO);
		List<MaterialItemVO> list = page.getResult();
		Long totalRows = page.getTotalCount();
		this.getRequest().setAttribute("chargeItemList", list);
		this.getRequest().setAttribute("totalRows", totalRows.intValue());

		//统计总的金额
		this.getRequest().setAttribute("countFee", dispatchFormManageService.getServItemTotalFee(dispatchFormId));


//		Page page = new Page();
//		List chargeItemList =initChargeItemList();
//		page.setResult(chargeItemList);
//		setAttribute("chargeItemList", chargeItemList);
//		setAttribute("totalRows", new Integer(chargeItemList.size()));

		return "chargeItemList";
	}

	/**
	 * 功能描述:导航到新增费用页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateChargeItemAdd(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){

		this.getRequest().setAttribute("flat", "add");
		this.getRequest().setAttribute("selectItemType", "1");
		return "chargeItemEdit";
	}

	/**
	 * 功能描述:保存新增费用<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String addChargeItem(@Read MaterialItemVO materialItemVO,
			@Read(key="serviceItemType")String serviceItemType){
		//物料
		if("1".equals(serviceItemType)){
			materialItemVO.setCreator(this.getUserId());
			dispatchFormManageService.addServItem(materialItemVO.getDispatchFormId(),materialItemVO);

		}
		//服务费
        if("2".equals(serviceItemType)){
        	LaborItemVO laborItemVO=new LaborItemVO();
        	PopulateUtil.populate(materialItemVO, laborItemVO);
        	laborItemVO.setChargeType("001");
        	laborItemVO.setCreator(this.getUserId());
        	dispatchFormManageService.addServItem(laborItemVO.getDispatchFormId(),laborItemVO);
		}

		this.getRequest().setAttribute("iframeName", "chargeItemList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:导航到修改费用页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateChargeItemEdit(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId,
			@Read(key="chargeItemId")@Out(key="chargeItemId")Long chargeItemId){
		AbstractServiceItemVO sbstractServiceItemVO=dispatchFormManageService.getAbstractServiceItemById(chargeItemId);
		if(sbstractServiceItemVO instanceof MaterialItemVO){
			MaterialItemVO materialItemVO=(MaterialItemVO)sbstractServiceItemVO;
			this.getRequest().setAttribute("entryVO", materialItemVO);
			this.getRequest().setAttribute("selectItemType", "1");
		}
		if(sbstractServiceItemVO instanceof LaborItemVO){
			LaborItemVO laborItemVO=(LaborItemVO)sbstractServiceItemVO;
			MaterialItemVO materialItemVO =new MaterialItemVO();
			PopulateUtil.populate(laborItemVO, materialItemVO);
			this.getRequest().setAttribute("entryVO", materialItemVO);
			this.getRequest().setAttribute("selectItemType", "2");
		}
	//	MaterialItemVO materialItemVO=(MaterialItemVO)dispatchFormManageService.getAbstractServiceItemById(chargeItemId);

		this.getRequest().setAttribute("flat", "edit");
		return "chargeItemEdit";
	}

	/**
	 * 功能描述:保存修改费用<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String editChargeItem(@Read MaterialItemVO materialItemVO,
			@Read(key="serviceItemType")String serviceItemType){
		if("1".equals(serviceItemType)){
			materialItemVO.setModifier(this.getUserId());
		  dispatchFormManageService.editServItem(materialItemVO.getDispatchFormId(),materialItemVO);
		}

		 if("2".equals(serviceItemType)){
			 LaborItemVO laborItemVO=new LaborItemVO();
	         PopulateUtil.populate(materialItemVO, laborItemVO);
	         laborItemVO.setChargeType("001");
	         laborItemVO.setModifier(this.getUserId());
			 dispatchFormManageService.editServItem(laborItemVO.getDispatchFormId(),laborItemVO);
		 }

		this.getRequest().setAttribute("iframeName", "chargeItemList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:删除费用清单<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void  deleteServItem(@Read(key="dispatchFormId")Long dispatchFormId,
			@Read(key="chargeItemId")Long chargeItemId){
		dispatchFormManageService.deleteServItem(dispatchFormId,chargeItemId);
		this.renderText("true");
	}


	/**
	 * 功能描述:造数据用的,service弄好了,就删除<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List initDispathchFormList(){
		List<DispatchFormVO> dispathchFormList = new ArrayList<DispatchFormVO>();

		for(int i = 0; i < 10; i++){
			DispatchFormVO dispatchFormVO =new DispatchFormVO();
			dispatchFormVO.setKeyword(Long.parseLong(""+i));
			dispatchFormVO.setDispatchNo(""+i);

			dispathchFormList.add(dispatchFormVO);
		}
		return dispathchFormList;
	}

	/**
	 * 功能描述:造数据用的,service弄好了,就删除<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List initChargeItemList(){
		List<MaterialItemVO> chargeItemList = new ArrayList<MaterialItemVO>();

		for(int i = 0; i < 5; i++){
			MaterialItemVO materialItemVO =new MaterialItemVO();
			materialItemVO.setKeyword(Long.parseLong(""+i));
			materialItemVO.setCode(""+i);

			chargeItemList.add(materialItemVO);
		}
		return chargeItemList;
	}

	/**
	 * 功能描述:接单登记
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String receiveDispatchForm(@Read DispatchingVO dispatchingVO,
			@Read(key="dispatchId")@Out(key="dispatchId")Long dispatchId,
			@Read(key="receiveTime")String receiveTime){
		dispatchingVO.setReceiveTime(DateUtil.convertStrToDate(receiveTime,"yyyy-MM-dd HH:mm"));
		dispatchFormManageService.dispatching(dispatchId,dispatchingVO);
		this.getRequest().setAttribute("iframeName", "customerDispatchFormList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:初始化接单登记
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-12-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String navigateReceiveDispatchForm(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){
		return "dispatchingAdd";
	}

	/**
	 * 功能描述:把紧急程度的列表放到request中去<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-24<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void getEmergencyDegreeList(){
		List<EmergencyDegreeVO> list=emergencyDegreeService.getEmergencyDegreeList();
		this.getRequest().setAttribute("emergencyDegreeList", list);

	}

	private IDFormManageService dispatchFormManageService;



	private IEmergencyDegreeService	emergencyDegreeService;

	private IEventManageService eventManageService; //客户事件Service

	private ICustServChargeService custServChargeService;

	public void setCustServChargeService(
			ICustServChargeService custServChargeService) {
		this.custServChargeService = custServChargeService;
	}

	public void setDispatchFormManageService(IDFormManageService dispatchFormManageService) {
		this.dispatchFormManageService = dispatchFormManageService;
	}


	public void setEmergencyDegreeService(
			IEmergencyDegreeService emergencyDegreeService) {
		this.emergencyDegreeService = emergencyDegreeService;
	}

	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}

	/**
	 * 功能描述:检查指定的事件是否能创建派工单 <br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-9<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkIsAllowAddDispatchForm(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId){
		eventManageService.checkIsAllowAddDispatchForm(customerEventId);
	}

	/**
	 * 功能描述:检查指定的派工单是否能作废  <br>
	 * @param dFormID
	 *
	 * Create author:许海堆<br>
	 * Create on:2009-1-9  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkIsAllowSetAsideDispatchForm(@Read(key="dispatchFormId")Long dispatchFormId){
		dispatchFormManageService.checkIsAllowSetAsideDispatchForm(dispatchFormId);
	}

   /**
    * 功能描述:检查指定的派工单是否允许接单登记  <br>
    * @param dispatchFormID 派工单ID
    *
    * Create author:许海堆<br>
    * Create on:2009-1-9  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
	public void checkIsAllowDispatching(@Read(key="dispatchFormId")Long dispatchFormId){
		dispatchFormManageService.checkIsAllowDispatching(dispatchFormId);
	}

	/**
    * 功能描述:检查指定的派工单是否允许增加收费项目  <br>
    * @param dispatchFormID 派工单ID
    *
    * Create author:许海堆 <br>
    * Create on:2009-1-9  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public void checkIsAllowAddServItem(@Read(key="dispatchFormId")Long dispatchFormId){
	   dispatchFormManageService.checkIsAllowAddServItem(dispatchFormId);
   }

   /**
    * 功能描述:检查指定的派工单是否允许修改服务收费项目  <br>
	 * @param dFormID 派工单ID
	 *
	 * Create author:许海堆 <br>
	 * Create on:2009-1-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	public void checkIsAllowEditServItem(@Read(key="dispatchFormId")Long dispatchFormId){
		 dispatchFormManageService.checkIsAllowEditServItem(dispatchFormId);
	}

	/**
	 * 功能描述:检查指定的派工单是否允许删除服务收费项目  <br>
	 * @param dFormID 派工单ID
	 *
	 * Create author:许海堆 <br>
	 * Create on:2009-1-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	public void checkIsAllowDeleteServItem(@Read(key="dispatchFormId")Long dispatchFormId){
		dispatchFormManageService.checkIsAllowDeleteServItem(dispatchFormId);
	}


}
